`:top
`!Grammar-based codes`! or `!grammar-based compression`! are `F33f`_`[compression`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Data_compression]`_`f algorithms based on the idea of constructing a `F33f`_`[context-free grammar`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Context-free_grammar]`_`f (CFG) for the string to be compressed. Examples include universal `F33f`_`[lossless data compression`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Lossless_data_compression]`_`f algorithms.`:cite-ref-1[`F5bf`_`[1`#cite-note-1]`_`f] To compress a data sequence x = x 1 ⋯ ⋯ x n {\\displaystyle x=x_{1}\\cdots x_{n}} , a grammar-based code transforms x {\\displaystyle x} into a context-free grammar G {\\displaystyle G} . The problem of finding a smallest grammar for an input sequence (`F33f`_`[smallest grammar problem`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Smallest_grammar_problem]`_`f) is known to be NP-hard,`:cite-ref-2[`F5bf`_`[2`#cite-note-2]`_`f] so many grammar-transform algorithms are proposed from theoretical and practical viewpoints. Generally, the produced grammar G {\\displaystyle G} is further compressed by statistical encoders like `F33f`_`[arithmetic coding`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Arithmetic_coding]`_`f.
>>Contents
• `F0af`_`[Examples and characteristics`#examples-and-characteristics]`_`f
• `F0af`_`[Practical algorithms`#practical-algorithms]`_`f
• `F0af`_`[See also`#see-also]`_`f
• `F0af`_`[References`#references]`_`f
• `F0af`_`[External links`#external-links]`_`f
-─
>>Examples and characteristics
The class of grammar-based codes is very broad. It includes `F33f`_`[block codes`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Block_code]`_`f, the multilevel pattern matching (MPM) algorithm,`:cite-ref-3[`F5bf`_`[3`#cite-note-3]`_`f] variations of the incremental parsing `F33f`_`[Lempel-Ziv code`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=LZ77_and_LZ78]`_`f,`:cite-ref-4[`F5bf`_`[4`#cite-note-4]`_`f] and many other new universal lossless compression algorithms. Grammar-based codes are universal in the sense that they can achieve asymptotically the `F33f`_`[entropy rate`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Entropy_rate]`_`f of any stationary, `F33f`_`[ergodic`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Ergodic]`_`f source with a finite alphabet.
>>Practical algorithms
The compression programs of the following are available from external links.
• `F33f`_`[Sequitur`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=SEQUITUR_algorithm]`_`f`:cite-ref-5[`F5bf`_`[5`#cite-note-5]`_`f] is a classical grammar compression algorithm that sequentially translates an input text into a CFG, and then the produced CFG is encoded by an arithmetic coder.
• `F33f`_`[Re-Pair`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Re-Pair]`_`f`:cite-ref-6[`F5bf`_`[6`#cite-note-6]`_`f] is a greedy algorithm using the strategy of most-frequent-first substitution. The compressive performance is powerful, although the main memory space requirement is very large.
• `F33f`_`[GLZA`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=GLZA]`_`f,`:cite-ref-7[`F5bf`_`[7`#cite-note-7]`_`f] which constructs a grammar that may be reducible, i.e., contain repeats, where the entropy-coding cost of "spelling out" the repeats is less than the cost creating and entropy-coding a rule to capture them. (In general, the compression-optimal SLG is not irreducible, and the Smallest Grammar Problem is different from the actual SLG compression problem.)
>>See also
• `F33f`_`[Dictionary coder`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Dictionary_coder]`_`f
• `F33f`_`[Grammar induction`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Grammar_induction]`_`f
• `F33f`_`[Straight-line grammar`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Straight-line_grammar]`_`f
>>References
`:cite-note-1`!1.`! `F0af`_`[↑`#cite-ref-1]`_`f `:citerefkiefferyang2000`aKieffer, J. C.; Yang, E.-H. (2000), "Grammar-based codes: A new class of universal lossless source codes", `*IEEE Trans. Inf. Theory`*, `!46`! (3): 737–754, `F33f`_`[doi`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Doi_(identifier)]`_`f:10.1109/18.841160
`:cite-note-2`!2.`! `F0af`_`[↑`#cite-ref-2]`_`f `:citerefcharikarlehmanliupanigrahy2005`aCharikar, M.; Lehman, E.; Liu, D.; Panigrahy, R.; Prabharakan, M.; Sahai, A.; Shelat, A. (2005), "The Smallest Grammar Problem", `*IEEE Trans. Inf. Theory`*, `!51`! (7): 2554–2576, `F33f`_`[doi`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Doi_(identifier)]`_`f:10.1109/tit.2005.850116, `F33f`_`[S2CID`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=S2CID_(identifier)]`_`f 6900082
`:cite-note-3`!3.`! `F0af`_`[↑`#cite-ref-3]`_`f `:citerefkiefferyangnelsoncosman2000`aKieffer, J. C.; Yang, E.-H.; Nelson, G.; Cosman, P. (2000), "Universal lossless compression via multilevel pattern matching", `*IEEE Trans. Inf. Theory`*, `!46`! (4): 1227–1245, `F33f`_`[doi`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Doi_(identifier)]`_`f:10.1109/18.850665, `F33f`_`[S2CID`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=S2CID_(identifier)]`_`f 8191526
`:cite-note-4`!4.`! `F0af`_`[↑`#cite-ref-4]`_`f `:citerefzivlempel1978`aZiv, J.; Lempel, A. (1978), "Compression of individual sequences via variable rate coding", `*IEEE Trans. Inf. Theory`*, `!24`! (5): 530–536, `F33f`_`[doi`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Doi_(identifier)]`_`f:10.1109/TIT.1978.1055934, `F33f`_`[hdl`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Hdl_(identifier)]`_`f:10338.dmlcz/142945
`:cite-note-5`!5.`! `F0af`_`[↑`#cite-ref-5]`_`f `:citerefnevill-manningwitten1997`aNevill-Manning, C. G.; Witten, I. H. (1997), "Identifying Hierarchical Structure in Sequences: A linear-time algorithm", `*Journal of Artificial Intelligence Research`*, `!7`! (4): 67–82, `F33f`_`[arXiv`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ArXiv_(identifier)]`_`f:cs/9709102, `F33f`_`[doi`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Doi_(identifier)]`_`f:10.1613/jair.374, `F33f`_`[hdl`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Hdl_(identifier)]`_`f:10289/1186, `F33f`_`[S2CID`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=S2CID_(identifier)]`_`f 2957960
`:cite-note-6`!6.`! `F0af`_`[↑`#cite-ref-6]`_`f `:citereflarssonmoffat2000`aLarsson, N. J.; Moffat, A. (2000), "Offline Dictionary-Based Compression" (PDF), `*Proceedings of the IEEE`*, `!88`! (11): 1722–1732, `F33f`_`[doi`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Doi_(identifier)]`_`f:10.1109/5.892708
`:cite-note-7`!7.`! `F0af`_`[↑`#cite-ref-7]`_`f `:citerefconradwilson2016`aConrad, Kennon J.; Wilson, Paul R. (2016). "Grammatical Ziv-Lempel Compression: Achieving PPM-Class Text Compression Ratios with LZ-Class Decompression Speed". `*2016 Data Compression Conference (DCC)`*. p. 586. `F33f`_`[doi`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Doi_(identifier)]`_`f:10.1109/DCC.2016.119. `F33f`_`[ISBN`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ISBN_(identifier)]`_`f 978-1-5090-1853-6. `F33f`_`[S2CID`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=S2CID_(identifier)]`_`f 3116024.
>>External links
• GLZA discussion and paper
• Description of grammar-based codes with example
• Sequitur codes Archived 2008-10-13 at the `F33f`_`[Wayback Machine`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Wayback_Machine]`_`f
• Re-Pair codes
• Re-Pair codes a version of Gonzalo Navarro.
• GrammarViz 2.0 - implementation of Sequitur, Re-Pair, and parallel Re-Pair in Java.
`c`F0af`_`[↑ Back to top`#top]`_`f`a